<template>
  <div class="container" ref="mainCon">
    <topTitle :titleText="titleText" :titleExplain="titleExplain"></topTitle>
    <div class="mainCon">
      <el-row class="toolbar searchInputWidth" style="padding-bottom: 0">
        <el-form :inline="true" :model="filterField" ref="searchConditions">
          <el-col :lg="8" :xl="6">
            <el-form-item label="区域" class="row-padding-bottom">
              <el-cascader
                ref="manageArea"
                v-model="areaId"
                :options="areasOptions"
                @change="getAreaId"
                clearable
                filterable
                :props="{ checkStrictly: true }"
                :append-to-body="false"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="行业" class="row-padding-bottom">
              <el-select
                v-model="filterField.industryId"
                clearable
                filterable
                placeholder="请选择"
                :popper-append-to-body="false"
              >
                <el-option
                  v-for="item in industryOptions"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :lg="8" :xl="6">
              <el-form-item label="施工方" class="row-padding-bottom">
                <el-input
                  v-model="filterField.pbName"
                  placeholder="请输入公司名称"
                  clearable
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="开工状态" class="row-padding-bottom">
                <el-select
                  v-model="filterField.status"
                  clearable
                  filterable
                  placeholder="请选择"
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in statusOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="项目名称" class="row-padding-bottom">
                <el-input
                  v-model="filterField.projectName"
                  placeholder="请输入项目名称"
                  clearable
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="清洗结果" class="row-padding-bottom">
                <el-select
                  v-model="filterField.cleanId"
                  clearable
                  filterable
                  placeholder="请选择"
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in cleanIdOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <!-- <el-form-item>
                        <el-button type="primary" icon="icon iconfont iconchazhao1" @click="searchList" class="commonBtn"></el-button>
                    </el-form-item> -->
          </template>
          <el-col :lg="8" :xl="6">
            <el-form-item label="">
              <a
                @click="toggleAdvanced"
                style="margin-left: 40px; color: #409eff; cursor: pointer"
                class="btnColor"
              >
                {{ advanced ? '收起' : '展开' }}
                <i
                  :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
                ></i>
              </a>
              <el-button
                type="primary"
                icon="icon iconfont iconchazhao1"
                @click="searchList"
                class="searchBtn"
              >
                <span>搜索</span>
              </el-button>
            </el-form-item>
            <el-form-item>
              <exportBtn
                :dataLen="tableList.length"
                :filterField="filterField"
                api="api-s/report/export_JC_CS_PROJECT_WASH"
              ></exportBtn>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <el-table
          ref="tableBox1"
          :max-height="tableHeight"
          :data="tableList"
          v-loading="loading"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          stripe
          style="border: 1px solid #dfe6ec"
        >
          <el-table-column prop="projectName" label="项目名称" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.projectName"
                :columnName="'projectName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="areaDetailDesc"
            label="地区"
            align="center"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="industryName"
            label="行业"
            align="center"
            :formatter="formatTd"
          ></el-table-column>
          <!-- <el-table-column prop="pbName" label="施工方" align="center" :formatter=formatTd></el-table-column>  -->
          <el-table-column label="施工方" align="left" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.pbName"
                :columnName="'pbName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="statusDesc"
            label="开工状态"
            align="center"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="cleanName"
            label="清洗结果"
            align="center"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column label="操作" align="left" fixed="right" width="110">
            <template slot-scope="scope">
              <el-link
                type="primary"
                v-hasBtn="'washProject:update'"
                @click="washOperate(scope.$index, scope.row)"
                >清洗处理</el-link
              >
              <!-- <span v-else>--</span> -->
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关信息"></emptyTable>
          </div>
        </el-table>
        <!-- filterField为搜索条件 -->
        <!-- getTableList为获取子组件的数据 -->
        <Pagination
          ref="pagination"
          @getpageSize="getpageSize"
          @getpageNo="getpageNo"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
        ></Pagination>
      </div>
      <el-dialog
        title="清洗处理"
        :visible.sync="formVisible"
        :close-on-click-modal="false"
        @close="cancelEdit"
        custom-class="brand-dialog-min-width"
        width="640px"
        :modal-append-to-body="false"
        :append-to-body="true"
      >
        <el-form
          :model="formData"
          :rules="formRules"
          ref="formData"
          :label-width="formLabelWidth"
        >
          <el-form-item label="清洗项目：">
            <span class="mWidth">{{ formData.projectName }}</span>
          </el-form-item>
          <el-form-item label="项目ID：">
            <span class="mWidth">{{ formData.id }}</span>
          </el-form-item>
          <el-form-item label="清洗处理：" prop="cleanId">
            <el-select
              v-model="formData.cleanId"
              placeholder="请选择"
              class="mWidth"
              clearable
              filterable
            >
              <el-option
                v-for="(item, index) of cleanIdOptions"
                :key="index"
                :label="item.name"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="清洗原因：" prop="cleanReason">
            <el-input
              type="textarea"
              class="mWidth"
              rows="3"
              v-model="formData.cleanReason"
              autocomplete="off"
              placeholder="请输入清洗原因"
              maxlength="200"
              show-word-limit
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="cancelEdit('formData')" class="cancel"
            >取消</el-button
          >
          <el-button
            type="primary"
            @click.native="saveInfo('formData')"
            class="save"
            :loading="isSaveLoading"
            >保存</el-button
          >
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import scrollTable from '@/mixin/scrollTable.js'
import { mapActions, mapState } from 'vuex'
export default {
  name: 'washProject',
  mixins: [scrollTable],
  data() {
    return {
      titleText: '项目清洗',
      advanced: false,
      titleExplain:
        '针对重复项目、测试项目、虚假项目等进行标记，此类项目不参与项目评级，视为废弃项目',
      formVisible: false, //项目清洗弹框
      nationOptions: [], //民族
      statusOptions: [], //开工状态
      cleanIdOptions: [], //清洗结果
      // cleanIdOptions: [],
      //表格数据
      tableList: [],
      requestUrl: 'api-p/project/expand/findprojectwashbymonitor',
      date: [],
      areaId: '', //区域
      //搜索条件
      filterField: {
        areaId: '', //区域
        industryId: '', //行业
        pbName: '', //施工方
        status: '', //开工状态
        projectName: '', //项目名称
        cleanId: '', //清洗结果
        serverArea: '', // 服务部署区域
      },
      pageSize: 20,
      pageNo: 1,
      formLabelWidth: '125px',
      // formData: "", //项目清洗获取的行的数据
      formData: {
        id: '',
        cleanId: '',
        cleanReason: '',
      },
      formRules: {
        cleanId: [
          {
            required: true,
            message: '清洗处理不能为空',
            trigger: 'change',
          },
        ],
        cleanReason: [
          {
            required: true,
            message: '请输入清洗原因',
            trigger: 'blur',
          },
        ],
      },
      employeeId: '', //务工id
      isShow: false, //列表加载
      loading: true,
      isSaveLoading: false,
      // isShowUpdate:""
    }
  },
  computed: { ...mapState(['areasOptions', 'industryOptions']) },

  created() {
    //获取行业
    this.getIndustry({})
    //获取区域
    this.getAreas({ areaId: this.$store.state.areaInfoArr.areaId })
    //获取状态
    this.getSelectData('PROJECT_STATUS')
    //获取处理类型
    this.getSelectData('PROJECT_CLEAN')
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
    this.filterField.serverArea = this.$store.state.areaInfoArr.areaId
    // this.isShowUpdate = this.$store.state.buttonLimitsArr.indexOf('washProject:update') > -1 ;
  },
  activated() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
    })
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    getpageSize(val) {
      this.pageSize = val
    },
    getpageNo(val) {
      this.pageNo = val
    },
    ...mapActions(['getAreas', 'getIndustry']),
    getSelectData(dicitemType) {
      this.axios({
        method: 'post',
        url: 'api-ps/creditProjectScore/expand/findprowashbytype',
        data: {
          type: dicitemType,
        },
      })
        .then((response) => {
          if (dicitemType == 'PROJECT_STATUS') {
            this.statusOptions = response.data.result
          } else if (dicitemType == 'PROJECT_CLEAN') {
            this.cleanIdOptions = response.data.result
          }
        })
        .catch((response) => {})
    },
    //获取选中的区域最后一级id
    getAreaId() {
      var areaId = this.areaId[this.areaId.length - 1]
      this.filterField.areaId = areaId
    },
    getTableList(data) {
      this.tableList = data
      this.loading = false
      if (this.tableList.length < 1) {
        this.isShow = true
      }
    },
    // 搜索
    searchList() {
      this.$refs.pagination.getTableListData()
    },
    //清洗处理
    washOperate(index, row) {
      this.formVisible = true
      this.formData = Object.assign({}, row)
    },
    cancelEdit() {
      this.formVisible = false
      this.$refs['formData'].resetFields()
    },
    saveInfo() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.saveEditInfo()
        } else {
          return false
        }
      })
    },
    saveEditInfo() {
      this.isSaveLoading = true
      this.axios({
        method: 'post',
        url: 'api-p/project/expand/editwash',
        data: {
          id: this.formData.id,
          cleanId: this.formData.cleanId,
          cleanReason: this.formData.cleanReason,
        },
      })
        .then((response) => {
          this.isSaveLoading = false
          if (!response.data.hasError) {
            this.$message({
              type: 'success',
              message: '保存成功',
            })
            this.$refs['formData'].resetFields()
            this.formVisible = false
            this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
          } else {
            this.$message({
              type: 'error',
              message: response.data.errorMsg,
            })
            this.isSaveLoading = false
          }
        })
        .catch((error) => {
          this.$message.error('保存失败')
          this.isSaveLoading = false
        })
    },
  },
}
</script>

<style lang="less" scoped>
.mainCon {
  top: 210px;
}
@media only screen and (max-width: 1366px) {
  /deep/.searchInputWidth .el-form--inline .el-form-item__label {
    width: 80px;
  }
}
@import '../../../static/css/filterArea.less';
</style>
